<?php
/**
*区域总监
*/
require("../include/init.php");
//取出北区的数据
$mysql=mysql::getIns();
date_default_timezone_set('Asia/Shanghai');


$sql="select areaname from area";
$area=$mysql->getAll($sql);
foreach($area as $v){
	$address=$v['areaname'];
	$maxtime=time();
	$mintime=$maxtime-10*60;
	// echo $maxtime . '<br />';
	// echo $mintime;
	$sql="select count(*) from alldev where whitelist <> '1' and timestamp > $mintime and timestamp < $maxtime and shop='". $address ."'";
	$allnow=$mysql->getOne($sql);
	// echo $allnow;
	// print_r($allnow);
	// exit;


	//统计当天累计人数数
	$t = time();
	$start = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t));
	$end = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t));
	$sql="select count(*) from alldev where whitelist <> '1' and timestamp > $start and timestamp < $end and shop='" . $address . "'";
	$allday=$mysql->getOne($sql);

	//首次今天首次进店人数,通过字段visittimes为1来进行判断
	$sql="select count(*) from alldev where whitelist <> '1' and visittimes=1 and timestamp > $mintime and timestamp < $maxtime and shop='".$address ."'";
	$first=$mysql->getOne($sql);

	//统计当前销售区
	$sql="select count(*) from alldev where whitelist <> '1' and timestamp > $mintime and timestamp < $maxtime and shop='" . $address ."' and area in ('展厅区','销售区','精品区')";
	$sale=$mysql->getOne($sql);
	// echo $sale;
	// print_r($sale);
	// exit;

	//统计当前售后区
	$service=$allnow-$sale;


	//今天多次进店人数
	$sql="select count(*) from alldev where whitelist <> '1' and visittimes <> 1 and timestamp > $mintime and timestamp < $maxtime and shop='" . $address ."'";
	$second=$mysql->getOne($sql);

	//统计登陆人数
	$sql="select count(*) from alldev where whitelist <> '1' and isOL=1 and timestamp > $mintime and timestamp < $maxtime and shop='" . $address ."'";
	$login=$mysql->getOne($sql);
	// echo $login;

	//统计未登陆人数
	$sql="select count(*) from alldev where whitelist <> '1' and isOL=2 and timestamp > $mintime and timestamp < $maxtime and shop='" . $address ."'";
	$nolog=$mysql->getOne($sql);

	//统计历史进店人数
	$sql="select count(*) from alldev where whitelist <> '1' and shop='". $address ."'";
	$history=$mysql->getOne($sql);
	switch ($address) {
    case '义乌市博奥汽车销售服务有限公司':
      $hash='4S_Area_main_ywba';
      break;
    case '广东粤奥汽车销售服务有限公司':
      $hash='4S_Area_main_gdya';
      break;
    case '辽宁鑫迪汽车销售服务有限公司':
      $hash='4S_Area_main_lnxd';
      break;
    case '江苏天奥汽车销售服务有限公司':
      $hash='4S_Area_main_jsta';
      break;
    case '成都新元素雅麓汽车销售服务有限公司':
      $hash='4S_Area_main_cdyl';
      break;
    case '云南驰泰汽车销售服务有限公司':
      $hash='4S_Area_main_ynct';
      break;
    case '宁夏奥立升汽车销售服务有限公司':
      $hash='4S_Area_main_nxals';
      break;
    case '陕西奥诚汽车销售服务有限公司':
      $hash='4S_Area_main_sxac';
      break;
    case '北京博瑞祥星汽车销售有限公司':
      $hash='4S_Area_main_brxx';
      break;
  }
	$redis = new Redis();
  //连接服务器
  $redis->connect("localhost");
  //授权
  $redis->auth("redis=pwd@2016");

  $redis->hmset($hash,
  array(
		'allnow' =>$allnow,
		'allday'=>$allday,
		'first'=>$first,
		'second'=>$second,
		'sale'=>$sale,
		'service'=>$service,
		'login'=>$login,
		'nolog'=>$nolog,
		'history'=>$history
	));
}
